package com.boot.main.utils;

import java.security.MessageDigest;

public class Md5 {
    //https://blog.csdn.net/YXWik/article/details/123573737
    private static final String KEY_MD5 = "MD5";
    private static final String[] strDigits = {"0", "1", "2", "3", "4", "5",
            "6", "7", "8", "9", "a", "b", "c", "d", "e", "f" };
    private static String byteToArrayString(byte bByte){
        int iRet = bByte;
        if(iRet < 0){
            iRet += 256;
        }
        int iD1 = iRet / 16;
        int iD2 = iRet % 16;
        return strDigits[iD1] + strDigits[iD2];
    }
    private static String byteToString(byte[] bByte){
        StringBuffer sBuffer = new StringBuffer();
        for(int i=0;i<bByte.length;i++){
            sBuffer.append(byteToArrayString(bByte[i]));
        }
        return sBuffer.toString();
    }
    public static String GetMD5Code(String strObj) throws Exception{
        MessageDigest md = MessageDigest.getInstance(KEY_MD5);
        return byteToString(md.digest(strObj.getBytes()));
    }
}
